<?php

class ctrl_ordersearch extends ctrl_base
{
    public static function index()
    {
        self::weixin_check_login();
        self::smarty()->assign('user_type', $_SESSION['wx_userinfo']['user_type']);
        self::smarty()->display('orderSearchIndex.html');
    }

    public static function orderInput()
    {
        self::smarty()->display('orderInput.html');
    }

    public static function orderSearch()
    {
        //根据关键字查询
        $visit_name = trim($_POST['visit_name']);
        $visit_no = trim($_POST['visit_no']);

        //$openId = $_SESSION['wx_userinfo']['openid'];
        $openId = '';
        $list = model_order::getOrderList($visit_name, $visit_no, $openId);

        if(count($list) == 1){
            header('location:/index.php?act=ordersearch&st=orderInfo&id='.$list[0]['order_id']);
        }

        //查询
        self::smarty()->assign("list", $list);
        self::smarty()->display('orderSearchList.html');
    }

    public static function orderInfo()
    {
        $id = intval(trim($_GET['id']));
        if ($id == 0) {
            header("Content-type: text/html; charset=utf-8");
            exit("非法访问！");
        }
        /*详情显示逻辑
        根据合同号找出所有订单，首先显示订单中任意一条的合同基本信息
        其次显示所有订单涉及的产品名称
        在产品下显示订单中涉及的材质
        在材质下显示 每个规格 的 具体订单数据*/
        //$openId = $_SESSION['wx_userinfo']['openid'];
        $openId = '';
        $agreement = model_order::getAgreementNo($id, $openId);
        if (empty($agreement)) {
            header("Content-type: text/html; charset=utf-8");
            exit("非法访问！");
        }
        //查询该合同下所有订单
        $list = model_order::getOrderListForAgreementNo($agreement, $openId);


        //根据produce_id 分组
        $produceList = array();
        $produceInfo = array();
        foreach ($list as $key => $value) {


            $value['agreement_sign_date'] = substr($value['agreement_sign_date'],0,10);
            $value['agreement_date'] = substr($value['agreement_date'],0,10);
            $value['arrive_date'] = substr($value['arrive_date'],0,10);
            $value['schedul_date'] = substr($value['schedul_date'],0,10);


            if(empty($produceInfo)){
                $produceInfo['agreement_no'] = $value['agreement_no'];
                $produceInfo['sales_name'] = $value['sales_name'];
                $produceInfo['sales_customer'] = $value['sales_customer'];
                $produceInfo['agreement_sign_date'] = $value['agreement_sign_date'];
                $produceInfo['agreement_date'] = $value['agreement_date'];
                $produceInfo['arrive_date'] = $value['arrive_date'];
            }

            //产品名称
            if(!array_key_exists($value['produce_name'],$produceList)){
                $produceList[$value['produce_name']]['produce_name'] = $value['produce_name'];
            }

            $produceList[$value['produce_name']]['list'][$value['material']][$key] = $value;

        }
        self::smarty()->assign("info", $produceInfo);
        self::smarty()->assign("list", $produceList);

        self::smarty()->display('orderSearchInfo.html');
    }
}
